﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using HYRevitCode.CurveUtility.ColinearCurveGroup;

namespace HYRevitCode.SampleAndTest
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CmdColinearGroup : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Document document = commandData.Application.ActiveUIDocument.Document;
			List<Element> list = new FilteredElementCollector(document).OfClass(typeof(CurveElement)).ToElements().ToList<Element>();
			List<Curve> list2 = new List<Curve>();
			foreach (Element element in list)
			{
				ModelCurve modelCurve = element as ModelCurve;
				if (modelCurve != null)
				{
					list2.Add(modelCurve.GeometryCurve);
				}
			}
			List<List<Curve>> colinearGroup = new ColinearGroupManager(list2).GetColinearGroup();
			using (Transaction transaction = new Transaction(document, "test"))
			{
				transaction.Start();
				foreach (Element element2 in list)
				{
					document.Delete(element2.Id);
				}
				foreach (List<Curve> list3 in colinearGroup)
				{
					foreach (Curve curve in list3)
					{
						document.Create.NewModelCurve(curve, document.ActiveView.SketchPlane);
					}
				}
				transaction.Commit();
			}
			TaskDialog.Show("提示", "分组连接成功");
			return Autodesk.Revit.UI.Result.Succeeded;
		}
	}
}
